<?php

namespace app\base\command;


use app\base\model\RedianData;
use app\base\model\RedianTask;
use app\base\service\TaskService;
use think\admin\Command;
use think\console\Input;
use think\console\Output;
/**
 * 数据导入
 * Class SmsNum
 * @package app\common\command
 */
class TaskRunB extends Command
{
    /**
     * 配置指令
     */
    protected function configure()
    {
        $this->setName('xbase:taskRun');
        $this->setDescription('TaskRun');
    }

    protected function execute(Input $input, Output $output)
    {
        $queue_data = $this->queue->data;
        $task_id = $queue_data['task_id'];
        $this->setQueueProgress("开始执行任务", 0);
        $task = RedianTask::mQuery()->where([ "id"=>$task_id])->find();
        if(empty($task)){
           // $this->error('任务不存在');
            $this->setQueueError("任务不存在");
        }
        if($task['type']==1){
            //开始采集任务
            $this->setQueueProgress("开始采集任务,请稍等...", 0);
            RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>1]);
            $res = TaskService::instance()->startGatherCoze($task);
            if($res['code']==0){
                $this->setQueueError("任务执行失败:采集失败");
                RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>7,'error_msg'=>"采集失败:".$res['msg']]);
            }else{
                sleep(30);
                $execute_id = $res['data'];
                $i = 0;
                while ( $i<60){
                    //采集状态查询
                    $this->setQueueProgress("查询采集结果,请稍等...".$execute_id, 0);
                    $res = TaskService::instance()->getWorkflowResult($task,$execute_id);
                    $this->setQueueProgress("第{$i}次采集结果,".json_encode($res), 0);
                    if($res==0){
                        $this->setQueueError("任务执行失败:查询采集结果失败");
                        RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>7,'error_msg'=>"查询采集结果失败:".$res['msg']]);
                    }elseif ($res==1){
                        //采集成功
                        break;
                    }else{
                        sleep(10);
                    }
                    $i++;
                }
            }


//
//            if(empty($redian_data)){
//                $this->setQueueError("任务执行失败:采集失败");
//
//                RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>7,'error_msg'=>"采集失败"]);
//            }

            //计算相似度
            $this->setQueueProgress("开始筛选文案,请稍等...", 20);
            RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>2]);
            $redian_data = TaskService::instance()->similarity($task);
            if(empty($redian_data)){
                $this->setQueueError("任务执行失败:相似度计算失败");
                RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>7,'error_msg'=>"相似度计算失败"]);
            }
            $out_redian_data = RedianData::mQuery()->where(['task_id'=>$task_id])->order('similarity desc')->limit(0,$task['out_num'])->select()->toArray();
            $out_redian_data_ids = array_column($out_redian_data,'id');
            RedianData::mQuery()->whereIn('id',$out_redian_data_ids)->update(['filter_status'=>1]);
            //口播文本生成
            $this->setQueueProgress("开始口播文本生成,请稍等...", 40);
            RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>3]);
            $redian_data = TaskService::instance()->textGenerate($task);
            if(empty($redian_data)){
                $this->setQueueError("任务执行失败:口播文本生成失败");
                RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>7,'error_msg'=>"口播文本生成失败"]);
            }
        }

        //音频生成
        $this->setQueueProgress("开始音频生成,请稍等...", 60);
        RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>4]);
        $redian_data = TaskService::instance()->audioGenerate($task);
        if(empty($redian_data)){
            $this->setQueueError("任务执行失败:音频生成失败");
            RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>7,'error_msg'=>"音频生成失败"]);
        }
        //视频生成
        $this->setQueueProgress("开始音成视频生成,请稍等...", 80);
        RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>5]);
        $redian_data = TaskService::instance()->videoGenerate($task);
        if(empty($redian_data)){
            $this->setQueueError("任务执行失败:视频生成失败");
            RedianTask::mQuery()->where([ "id"=>$task_id])->update(['progress'=>7,'error_msg'=>"视频生成失败"]);
        }

        //创建视频生成状态查询任务
        sysqueue($task['title']."视频生成状态查询任务", "xbase:heygenRetrieve",  10, $data = [
            'task_id'=>$task_id,
        ], $rscript = 1, $loops = 0);



        $this->setQueueProgress("任务完成", 100);
        $this->setQueueSuccess("任务完成");


    }



}
